clear all
set more off, perm
set maxvar 120000
set varabbrev off
* ---------------------------------------------- *
global dir 	"`1'"
global Data 	$dir/Data
global Tables 	$dir/Tables
global Figures 	$dir/Figures
global Work 	$dir/Work
global Temp 	$dir/Temp
global Pseudo	$dir/Work/Temp
* ---------------------------------------------- *
use $Work/EBR_new_monthly, clear
drop SIZE
rename SI10 SIZE
keep permco permno datem BOOK SIZE INV* PROF MOME qruf1 qnic* ret_mo*
gstats winsor qnic*, cuts(1 99) by(datem) replace
* -------------------------------------------- *
tempfile tmpstuff
save "`tmpstuff'", replace
* -------------------------------------------- *
foreach PORTF in BOOK {	
use if `PORTF'+qruf1<. using "`tmpstuff'", clear
generate portf=1+int((`PORTF'-1)/2)
order *, sequential    
keep permco permno datem portf ret_mo1-ret_mo60 qnic*
gcollapse (mean) ret_mo1-ret_mo60 qnic*, by(datem portf)
* -------------------------------------------- *
gen  cumNrawMo1=ret_mo1-1
gen  cumQrawMo1=qnic1
* -------------------------------------------- *
gen  cumNrawQr1=ret_mo1*ret_mo2*ret_mo3-1
gen  cumQrawQr1=(1+qnic1)*(1+qnic2)*(1+qnic3)-1
* -------------------------------------------- *
gen  cumNrawYr1=1 
gen  cumQrawYr1=1 
forvalues x=1(1)12 {
	replace cumNrawYr1=cumNrawYr1*ret_mo`x'
	replace cumQrawYr1=cumQrawYr1*(1+qnic`x')
}
replace cumNrawYr1=cumNrawYr1-1
replace cumQrawYr1=cumQrawYr1-1
* -------------------------------------------- *
gen  cumNrawYr3=1 
gen  cumQrawYr3=1 
forvalues x=1(1)36 {
	replace cumNrawYr3=cumNrawYr3*ret_mo`x'
	replace cumQrawYr3=cumQrawYr3*(1+qnic`x')
}
replace cumNrawYr3=cumNrawYr3-1
replace cumQrawYr3=cumQrawYr3-1
* -------------------------------------------- *
gen  cumNrawYr5=1 
gen  cumQrawYr5=1 
forvalues x=1(1)60 {
	replace cumNrawYr5=cumNrawYr5*ret_mo`x'
	replace cumQrawYr5=cumQrawYr5*(1+qnic`x')
}
replace cumNrawYr5=cumNrawYr5-1
replace cumQrawYr5=cumQrawYr5-1
* -------------------------------------------- *
keep portf datem cumN* cumQ* 

tempfile tmp
save "`tmp'", replace
*********************
keep if portf==1 | portf==5 

foreach var of varlist cum* {
replace `var'=-`var' if portf==5 & index("`PORTF'","INV")+index("`PORTF'","SIZE")==1
replace `var'=-`var' if portf==1 & index("`PORTF'","INV")+index("`PORTF'","SIZE")==0
}
foreach var of varlist cum*  {
	bys datem: egen n`var'=count(`var')
}
foreach var of varlist cum* {
	replace `var'=. if n`var'<2
	drop n`var'
}
gcollapse (sum)        cum* ,  by(datem)
foreach var of varlist cum*  {
	replace `var'=. if `var'==0
}
generate portf=6

append using `tmp'
sort portf datem
*********************
tsset portf datem
eststo clear
gen cumQ=.
foreach p of numlist 6 {
replace cumQ=cumQrawMo1
eststo new_`p'_1: quietly: ivreg2 cumNrawMo1 cumQ  	if portf==`p', bw(1 )
replace cumQ=cumQrawQr1
eststo new_`p'_2: quietly: ivreg2 cumNrawQr1 cumQ  	if portf==`p', bw(4 )
replace cumQ=cumQrawYr1
eststo new_`p'_3: quietly: ivreg2 cumNrawYr1 cumQ  	if portf==`p', bw(13)
replace cumQ=cumQrawYr3
eststo new_`p'_4: quietly: ivreg2 cumNrawYr3 cumQ 	if portf==`p', bw(37)
replace cumQ=cumQrawYr5
eststo new_`p'_5: quietly: ivreg2 cumNrawYr5 cumQ 	if portf==`p', bw(61)
}
local file "AppendixB12_June"
if "`PORTF'"=="BOOK" {
esttab new* using $Tables/`file'.csv, nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2_a) replace  title(`PORTF')
}
if "`PORTF'"!="BOOK" {
esttab new* using $Tables/`file'.csv, nonotes compress b(4) se(4) star(c 0.10 b 0.05 a 0.01) nolabel nogap stats(N r2_a) append   title(`PORTF')
}
}
